package com.hexacta.hrs.domain.staffing;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.vectrics.cv.PersonSkill;
import org.vectrics.employee.Employee;


public class RequirementCalculator implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private Double disponibilityPercentage = 0.40d;
	private Double seniorityPercentage = 0.25d;
	private Double skillsPercentage = 0.35d;
	
	public RequirementResult createRequirementResult(final Employee employee,final  Requirement requirement,final  Integer disponibilityMonths, Calendar availableDate){
		return new RequirementResult(employee, disponibilityMonths, availableDate, totalResult(employee, requirement, disponibilityMonths), getEmployeeSkills(requirement, employee));
	}
	
	private List<PersonSkill> getEmployeeSkills(final Requirement requirement, final Employee employee) {
		ArrayList<PersonSkill> personSkills = new ArrayList<PersonSkill>(employee.getPerson().getSkills());
		ArrayList<PersonSkill> resultList = new ArrayList<PersonSkill>();
		for (RequirementSkills skills : requirement.getRequirementSkills()) {
			for (PersonSkill personSkill : personSkills) {
				if ((personSkill.getSkill().getParentSkill()
						.compareTo(skills.getSkill().getParentSkill()) == 0)
						&& (personSkill.getSkill().compareTo(skills.getSkill()) == 0)) {
					resultList.add(personSkill);
				}
			}
		}
		return resultList;
	}

	public Double totalResult(final Employee employee,final Requirement requirement,final Integer disponibilityMonths){
		return disponibilityResult(disponibilityMonths) + seniorityResult(requirement, employee) + skillsResult(requirement, employee);
	}
	
	public Double disponibilityResult(Integer disponibilityMonths){
		return (double) (disponibilityMonths == 0 ? 100 : disponibilityMonths == 1 ? 50 : 0) * disponibilityPercentage;
	}
	
	public Double seniorityResult(final Requirement requirement, final Employee employee){
		employee.getFunction().getJobFunctionType().getArea().equalsIgnoreCase(requirement.getRequiredRol().getJobFunctionType().getArea());
		
		Integer employeeWeight = employee.getFunction().getWeight();
		Integer requirementWeight = requirement.getRequiredRol().getWeight();
		
		Double total = (double) ( employeeWeight == requirementWeight ? 100 : (employeeWeight+1) == requirementWeight ? 50 
				: (employeeWeight-1) == requirementWeight ? 50 : 0 );
		return total * seniorityPercentage;
	}
	
	public Double skillsResult(final Requirement requirement, final Employee employee){
		Double total = 0d;
		ArrayList<PersonSkill> personSkills = new ArrayList<PersonSkill>(employee.getPerson().getSkills());
		try {
			for (RequirementSkills skills : requirement.getRequirementSkills()) {
				Double levelPerson = 0d;
				Double level = 0d;
				level = skills.getLevel().getId().doubleValue();
				for (PersonSkill personSkill : personSkills) {
					if ((personSkill.getSkill().getParentSkill().compareTo(skills.getSkill().getParentSkill()) == 0)
							&&(personSkill.getSkill().compareTo(skills.getSkill()) == 0)) {
						levelPerson = personSkill.getLevel().getId()
								.doubleValue();
					}

				}
				total += (double) (levelPerson >= level ? 100
						: levelPerson + 1 == level ? 50 : 0)
						* (skills.getPercentage()/100);
			}
		} catch (Exception e) {
			return 0d;
		}
		return total * skillsPercentage;
	}
	
	public Double getDisponibilityPercentage() {
		return disponibilityPercentage;
	}
	public void setDisponibilityPercentage(Double disponibilityPercentage) {
		this.disponibilityPercentage = disponibilityPercentage;
	}
	public Double getSeniorityPercentage() {
		return seniorityPercentage;
	}
	public void setSeniorityPercentage(Double seniorityPercentage) {
		this.seniorityPercentage = seniorityPercentage;
	}
	public Double getSkillsPercentage() {
		return skillsPercentage;
	}
	public void setSkillsPercentage(Double skillsPercentage) {
		this.skillsPercentage = skillsPercentage;
	}
	
}
